<?php
namespace App\Api\Controllers;

use App\Api\Models\Albums;
use App\Http\Controllers\Controller;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;
use App\Api\Models\Goods;
use App\Api\Models\GoodsCategory;
use App\Api\Models\CmcFooter;
use Illuminate\Support\Facades\Redis;

class HomepageController extends Controller
{
    /**
     * 头部选项卡名称，唯一标识符
     * @return array
     */
    public function getHeadTab()
    {
        //需要获取的字段
        $allowField = [
            'category_name',
            'category_id',
            'pid'
        ];

        //调用模型层方法
        $categoryData = GoodsCategory::getCategoryName($allowField);

        $count = count($categoryData);

        for($i=0;$i<$count;$i++){
            $categoryData[$i]['file'] = config('hadtab.liu').$categoryData[$i]['category_id'];
            Redis::set('head'.$i,json_encode($categoryData[$i]));
        }

        $cateData = [];

        for($j=0;$j<$count;$j++){
            $cateData[] = json_decode(Redis::get('head'.$j),true);
        }

        if($cateData){
            return JsonService::success($categoryData);
        }
        return JsonService::fail('查询数据为空');
    }

    /**
     * 首页对应分类信息
     * @param $pid 父类id
     * @return array
     */
    public function getTabList(Request $request)
    {
        $validate = $request->validate([
            'category_id'=>['required','alpha_num']
        ]);

        //调用模型层方法
        $tabList = GoodsCategory::getGoodsValue($validate['category_id']);


        if($tabList){
            return JsonService::success($tabList);
        }
        return JsonService::fail('查询数据为空','',1003);
    }

    /**
     * 轮播图查询
     * @return array
     */
    public function getAlbumsFile()
    {
        //需要获取的字段
        $allowField = [
            'album_id',
            'album_name',
            'cover',
            'desc'
        ];

        //调用模型层方法
        $AlbumsData = Albums::pictureQuery($allowField);

        if($AlbumsData){
            return JsonService::success($AlbumsData);
        }
        return JsonService::fail('查询数据为空','',1003);
    }

    /**
     * 爆款模块选项卡信息
     * @return array
     */
    public function getTabCard()
    {
        $allowField = [
            'goods_class',
            'goods_class_name'
        ];

        $tabCard = Goods::getTabInformation($allowField);

        if($tabCard){
            return JsonService::success($tabCard);
        }
        return JsonService::fail('查询失败','',1003);
    }

    /**
     * 爆款模块选项卡
     * @param Request $request
     * @return array
     */
    public function getCardData(Request $request)
    {
        //需要获取的字段
        $allowField = [
            'goods_name',
            'price',
            'sale_num',
            'goods_image'
        ];

        $validate = $request->validate(['hid'=>['required','alpha_num']]);

        //调用模型层方法
        $goodsOption = Goods::getGoodsTab($validate['hid'],$allowField);

        if($goodsOption != []){
            return JsonService::success($goodsOption);
        }
        return JsonService::fail('查询到的数据为空','',1003);
    }

    /**
     * 优惠商品展示栏
     * @param Request $request
     * @return array
     */
    public function getRankingList(Request $request)
    {
        $allowField = [
            'goods_name',
            'price',
            'sale_num'
        ];

        $validate = $request->validate(['hid'=>['required','alpha_num']]);

        //请求模型层
        $rankingData = Goods::getRankingData($validate['hid'],$allowField);

        if($rankingData){
            return JsonService::success($rankingData);
        }
        return JsonService::fail('查询数据为空','',1003);
    }

    /**
     * 页脚信息查询
     * @return array
     */
    public function getFooterList()
    {
        $allowField = [
            'footer_title',
            'footer_url'
        ];

        $footerData = CmcFooter::getFooterData($allowField);

        if($footerData){
            return JsonService::success($footerData);
        }
        return JsonService::fail('加载失败，请重试','',1003);
    }
    
}
